package com.zhuiyun.pm.client.home.service.impl;

import com.zhuiyun.pm.client.home.mapper.MyBidMapper;
import com.zhuiyun.pm.client.home.pojo.dto.MyBidDTO;
import com.zhuiyun.pm.client.home.service.MyBidService;
import com.zhuiyun.pm.client.order.entity.ClientOrder;
import com.zhuiyun.pm.page.MyPageHelper;
import com.zhuiyun.pm.page.PageRequest;
import com.zhuiyun.pm.result.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Description TODO 我的竞价
 * @ClassName MyBidServiceImpl
 * @Author HH
 * @Date 2023/3/23 16:27
 **/
@Service
public class MyBidServiceImpl implements MyBidService {

    @Autowired
    MyBidMapper myBidMapper;

    /**
     * @Description 查询我的竞价
     * @Author HH
     * @Date 2023/3/24 10:00
     * @Return com.zhuiyun.pm.result.CommonResult
     * @param userId
     * @param pageRequest
     */
    @Override
    public CommonResult selectMyBidById(Integer userId, PageRequest pageRequest) {
        // 调用分页用的
        MyPageHelper.startPage(pageRequest);
        List<MyBidDTO> myBidDTOList = myBidMapper.selectMyBidById(userId);
        if(myBidDTOList != null && myBidDTOList.size() > 0){
            return CommonResult.ok(MyPageHelper.pageInfo(myBidDTOList));
        }else {
            return CommonResult.error("500");
        }
    }

    /**
     * @Description 查询我的订单
     * @Author HH
     * @Date 2023/3/24 13:34
     * @Return com.zhuiyun.pm.result.CommonResult
     * @param bidder
     * @param pageRequest
     */
    @Override
    public CommonResult selectMyOrderById(Integer bidder, PageRequest pageRequest) {
        Example example = new Example(ClientOrder.class);
        example.createCriteria().andEqualTo("bidder",bidder);
        // 调用分页用的
        MyPageHelper.startPage(pageRequest);
        List<ClientOrder> clientOrderList = myBidMapper.selectByExample(example);
        if(clientOrderList != null && clientOrderList.size() > 0){
            return CommonResult.ok(MyPageHelper.pageInfo(clientOrderList));
        }else {
            return CommonResult.error("500");
        }
    }

    /**
     * @Description 根据订单id修改订单状态
     * @Author HH
     * @Date 2023/3/24 16:05
     * @Return int
     * @param clientOrder
     */
    @Override
    public CommonResult updateMyOrderById(ClientOrder clientOrder) {
        // 将订单状态改为代发货
        clientOrder.setOrderFlag(2);
        int i = myBidMapper.updateMyOrderById(clientOrder);
        System.out.println(i);
        if(i > 0){
            return CommonResult.ok();
        }else {
            return CommonResult.error("500");
        }
    }
}
